package com.zzptc.sgz.shop.controller;

import com.zzptc.sgz.shop.dao.*;
import com.zzptc.sgz.shop.vo.Product;
import com.zzptc.sgz.shop.vo.User;
import com.zzptc.sgz.shop.vo.UserFoot;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@javax.servlet.annotation.WebServlet(name = "ProductListServlet",value = "/ProductListServlet")
public class ProductListServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request,
                          javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(javax.servlet.http.HttpServletRequest request,
                         javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String pid=request.getParameter("pid");
        String pcid=request.getParameter("pcid");
        String pcpid=request.getParameter("pcpid");
        System.out.println(pid+pcid);
        UserFootDaoI ufdao=new UserFootDaoImpl();
        ProductDaoI dao=new ProductDaoImpl();
        List<Product> userfootList=null;
        Product pro=null;
        HttpSession session = request.getSession();

        //从session获取登陆信息
        Object obj=session.getAttribute("user");
        if(obj!=null){
            User u=(User)obj;
            UserFoot uf=new UserFoot();
            uf.setpId(Integer.parseInt(pid));
            uf.setPcId(Long.parseLong(pcid));
            uf.setPcPId(Long.parseLong(pcpid));
            uf.setuId(u.getuId());
            try {
                //将用户足迹录入数据库
                 ufdao.insert(uf);
                //更新用户足迹，建议去重
                userfootList = dao.getAllUserFoot(u.getuId());
                session.setAttribute("userfootList",userfootList);

            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }

        try {
            pro=   dao.getproduct(Integer.parseInt(pid));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        session.setAttribute("pro",pro);

        request.getRequestDispatcher("product-list.jsp").forward(request,response);

    }
}
